package com.pc.scrs.controller;

import com.pc.scrs.bean.vo.CourseVo;
import com.pc.scrs.bean.vo.pageInfo.CourseListVoPageInfo;
import com.pc.scrs.service.ICourseService;
import com.pc.scrs.utils.ResponseResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Resource
    private ICourseService courseService;

    @GetMapping("/courseList")
    public ResponseResult queryCourseList(
            @RequestParam(value = "title", required = false, defaultValue = "") String title,
            @RequestParam(value = "subjectId", required = false, defaultValue = "") Integer subjectId,
            @RequestParam(value = "enable", required = false, defaultValue = "") Integer enable,
            @RequestParam(value = "required", required = false, defaultValue = "") Integer required,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "7") Integer pageSize
    ){
        CourseListVoPageInfo pageInfo = courseService.courseList(pageNum, pageSize, title, subjectId, enable, required);
        if (pageInfo != null) {
            return new ResponseResult(200, "ok", pageInfo);
        } else {
            return ResponseResult.FAIL;
        }
    }

    /**
     * 验证下架课程权限
     * @return
     */
    @GetMapping ("/showDisableAuthority")
    @PreAuthorize("hasAuthority('course:down')")
    public ResponseResult disableAuthority(){
        return new ResponseResult<>(200, "ok");
    }

    @GetMapping("/disable")
    public ResponseResult disableCourse(Integer id){
        Integer result = courseService.disableCourse(id);
        return new ResponseResult(200, "ok",result);
    }

    /**
     * 验证上架课程权限
     * @return
     */
    @GetMapping ("/showEnableAuthority")
    @PreAuthorize("hasAuthority('course:up')")
    public ResponseResult enableAuthority(){
        return new ResponseResult<>(200, "ok");
    }

    @GetMapping("/enable")
    public ResponseResult enableCourse(Integer id){
        Integer result = courseService.enableCourse(id);
        return new ResponseResult(200, "ok",result);
    }

    @GetMapping("/queryCourseById")
    public ResponseResult queryCourseById(Integer id){
        CourseVo courseVo = courseService.queryCourseById(id);
        return new ResponseResult(200, "ok",courseVo);
    }

    /**
     * 验证修改课程信息权限
     * @return
     */
    @GetMapping ("/showUpdateProfileAuthority")
    @PreAuthorize("hasAuthority('course:updateProfile')")
    public ResponseResult updateProfileAuthority(){
        return new ResponseResult<>(200, "ok");
    }


    @PostMapping("/updateProfile")
    public ResponseResult updateCourse(@RequestBody CourseVo courseVo){
        Integer result = courseService.updateCourse(courseVo);
        if(result>0){
            return new ResponseResult(200, "ok");
        }else {
            return ResponseResult.FAIL;
        }
    }

    @GetMapping("/showAddAuthority")
    @PreAuthorize("hasAuthority('course:add')")
    public ResponseResult addCourseAuthority(){
        return new ResponseResult(200, "ok");
    }
}
